package cn.fighter3.controller;

import cn.fighter3.dto.LoginDTO;
import cn.fighter3.entity.Iplog;
import cn.fighter3.mapper.IplogMapper;
import cn.fighter3.result.Result;
import cn.fighter3.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: 三分恶
 * @Date: 2021/1/17
 * @Description: TODO
 **/
@RestController()
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    IplogMapper iplogMapper;

    @PostMapping(value = "/api/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        String username = loginDTO.getLoginName();
        //Iplog iplog = new Iplog();
//        String.valueOf(new Date())
        //iplog.setLoginTime("2021-3-28");
        //iplog.setLogintime(String.valueOf(new Date()));
        //iplog.setUsername(username);
        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "localhost";
        }
        //iplog.setIp(ip);
        String state;
        Result result=loginService.login(loginDTO);
        if (result.getCode()==200){
            //iplog.setState(Iplog.STATE_FAIL);
            state="登录成功";
        }else{
            //iplog.setState(Iplog.STATE_SUCCESS);
            state="登录失败";
        }
        //@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
        Date time = new Date();
        String localString=time.toLocaleString();
        Iplog iplog=new Iplog(ip,username,state,localString);
        iplogMapper.insert(iplog);
        return result;
    }

    @PostMapping(value = "/api/hello")
    public Result hello(){
        return new Result(200,"hello","world");
    }

    @PostMapping(value = "/api/ip")
    public Result getip(){
        HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "localhost";
        }

        System.out.println("ippppppppp");
        System.out.println(ip);
        return new Result(200,"获取ip成功",ip);
    }
}
